package com.vancouver.knows.ui.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.my.MyFeedbackAboutProcotolActivity;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.EmailSendSecurityCodeTaskMark;

public class EmailSecurityCodeActivity extends BaseActivity implements IResultReceiver{

	private Context mContext;
	@Bind(R.id.common_tips)
	TextView  mCommonTips;
	@Bind(R.id.common_back)
	ImageView mCommonBack;
	@Bind(R.id.get_code)
	TextView mEmailCode;
	@Bind(R.id.email)
	MyAutoCompleteTextView mEmailEdit;
	@Bind(R.id.user_agreement)
	TextView userAgreement;
	private LoadingDialog loadingDialog;
	private String[] mAttrTitles;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_email_get_code);
		mContext = EmailSecurityCodeActivity.this;
		setViews();
		setListeners();
	}

	private void setViews() {
		loadingDialog = new LoadingDialog(mContext);
		Intent intent = getIntent();
		String tips = intent.getStringExtra("COMMON_TIPS");
		mCommonTips.setText(tips);
	}
	
	private void setListeners(){
		mCommonBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@OnClick(R.id.user_agreement)
	public void onUserAgreementClick(){
		mAttrTitles = mContext.getResources().getStringArray(R.array.my_list_titles);
		Intent intent = new Intent(mContext, MyFeedbackAboutProcotolActivity.class);
		if (mAttrTitles != null)
			intent.putExtra("USER_NOTICE", mAttrTitles[6]);
		intent.putExtra("PAGE_TYPE", "user_terms");
		startActivity(intent);
	}
	
	@OnClick(R.id.get_code)
	public void onEmailCodeClick() {
		if (mEmailEdit.getText().toString().trim().equals("")){
			Toast.makeText(mContext, mContext.getString(R.string.email_format_tips), Toast.LENGTH_SHORT).show();
			return;
		}
		
		BusinessModule bModule = imContext.getBusinessModule();
		EmailSendSecurityCodeTaskMark taskMark = bModule.getTaskMarkPool().getEmailSendSecurityCodeTaskMarkList();
		//发送登录需要的参数
		bModule.getServiceWrapper().userEmailSendSecurityCode(EmailSecurityCodeActivity.this, taskMark, mEmailEdit.getText().toString().trim());
		if (loadingDialog != null)
			loadingDialog.showLoadingDialog(mContext.getString(R.string.send_code_loading));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadingDialog != null){
			loadingDialog.dismiss();
		}
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if (loadingDialog != null)
			loadingDialog.closeLoadingDialog();
		if (exception != null && !exception.getExMessage().equals("")){
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_SHORT).show();
		}else{
			Intent intent = new Intent(mContext,  RegisterActivity.class);
			intent.putExtra("COMMON_TIPS", mContext.getString(R.string.email_register).toString());
			intent.putExtra("REGISTER_COUNT", mEmailEdit.getText().toString().trim());
			intent.putExtra("FROM_JUMP",  PhoConstants.CURRENT_OPEN_FROM_REGISTER_EMAIL);
			mContext.startActivity(intent);
			finish();
		}
	}
	

}